package com.example.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.AbstractNameValueGatewayFilterFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.UUID;


///**zidingyi
// * 自定义过滤器工厂，想要实现- OnceToken=X-Response-Token, uuid，，选择什么类型就返回什么类型的令牌
// */
//
//public class OnceTokenGatewayFilterFactory extends AbstractGatewayFilterFactory {
//    @Override
//    public GatewayFilter apply(Object config) {
//        return new GatewayFilter() {
//            @Override
//            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//                // 每次响应之前，添加一个一次性令牌
//                return chain.filter(exchange).then(Mono.fromRunnable(() -> {
//                    ServerHttpResponse response = exchange.getResponse();
//                    HttpHeaders headers = response.getHeaders();
//                    String value = config.getValue();
//
//                    if ("uuid".equalsIgnoreCase(value)) {
//                        value = UUID.randomUUID().toString();
//                    } else if ("jwt".equalsIgnoreCase(value)) {
//                        value = "";
//                    }
//
//                    headers.add(config.getName(), value);
//                }));
//            }
//        };
//    }
//
//}
